<?php

namespace app\admin\controller;




use app\models\CampusModel;
use app\models\CourseModel;
use app\models\GradesDetailsListsModel;
use app\models\GradesDetailsModel;
use app\models\StudentModel;
use app\models\StudentSubjectInfosClassModel;
use app\models\TeacherModel;
use app\models\UserModel;

class Elimination extends Common
{

    /**
     * 用户管理模型.
     *
     * @var UserModel
     */
    protected $userModel;

    /**
     * 教师管理模型.
     *
     * @var TeacherModel
     */
    protected $teacherModel;

    /**
     * 学员管理模型.
     *
     * @var StudentModel
     */
    protected $studentModel;

    /**
     * 班级管理详情模型.
     *
     * @var GradesDetailsModel
     */
    protected $gradesdetailModel;

    /**
     * CourseModel 课程模型.
     *
     * @var CourseModel
     */
    protected $courseModel;


    /**
     * CampusModel 校区管理模型.
     *
     * @var CampusModel
     */
    protected $campusModel;

    /**
     * 学生分班排课管理模型.
     *
     * @var StudentSubjectInfosClassModel
     */
    protected $studentsubjectinfosclassModel;

    public function lists()
    {
        if ($this->request->isAjax() && $this->request->isPost()) {
            if(input('type') == 1){  //按班级总览
                $param = $this->request->param();
                $param['type'] = 1001;
                $result = $this->gradesdetailModel->getClassManageLists($param);
                $this->success('success', '', $result, 0);
            }else if(input('type') == 2){  //按学员
                $param = $this->request->param();
                $result = $this->studentModel->getStudentSingUpLists($param);
                $this->success('success', '', $result, 0);
            }else if(input('type') == 3){  //按老师
                $param = $this->request->param();
                $result = $this->teacherModel->getTeachertLists($param);
                $this->success('success', '', $result, 0);
            }else if(input('type') == 4){  //班主任
                $param = $this->request->param();
                $result = $this->userModel->getHeadMasterUserLists($param);
                $this->success('success', '', $result, 0);
            }else if(input('type') == 5){  //课消明细
                $param = $this->request->param();
                $result = $this->studentsubjectinfosclassModel->getInfoClassLists($param);
                $this->success('success', '', $result, 0);
            }

        }
        //校区课消报表
        $data_campus_elimination = [];
		$where = [];
		if(get_current_type() == 4 || get_current_type() == 5){/*校区数据权限*/
			$where['id'] = get_current_campus_id();
		}elseif (get_current_type() == 2 || get_current_type() == 3){/*区域数据权限*/
			$where['id'] = ['in',get_current_campus_id()];
		}
		$where['status'] = 1;
        $campus_name = $this->campusModel->where($where)->select();

        //获取课消报表
        $where_elimination = [];
        $where_elimination['status'] = 1;
        $where_elimination['give_status'] = 1;  //赠送不算课消

        $where_ = [];
        $where_course = [];
		$where_grades = [];
        $where_['status'] = 1;
        $where_['give_status'] = 1;  //赠送不算课消
		$where_grades['status'] = 1;
		$where_grades['give_status'] = 1;  //赠送不算课消
        $where_course['status'] = 1;

        if(get_current_type() == 5){
            if(get_current_group_type() == 2){  //老师类
                $grades_res = $this->gradesdetailModel->where(['user_teacher_id' => get_current_id()])->column('id');
                $grades_res_str = implode(',',$grades_res);
                $where_elimination['class_subject_id'] = ['in',$grades_res_str];
                $where_['class_subject_id'] = ['in',$grades_res_str];
				$where_grades['id'] = ['in',$grades_res_str];

                $course_res = $this->gradesdetailModel->where(['user_teacher_id' => get_current_id()])->column('course_id');
                $course_res_str = implode(',',$course_res);
                $where_course['id'] = ['in',$course_res_str];
            }else if(get_current_group_type() == 3){  //助教类
                $grades_res = $this->gradesdetailModel->where(['user_assistant_id' => get_current_id()])->column('id');
                $grades_res_str = implode(',',$grades_res);
                $where_elimination['class_subject_id'] = ['in',$grades_res_str];
                $where_['class_subject_id'] = ['in',$grades_res_str];
				$where_grades['id'] = ['in',$grades_res_str];

                $course_res = $this->gradesdetailModel->where(['user_assistant_id' => get_current_id()])->column('course_id');
                $course_res_str = implode(',',$course_res);
                $where_course['id'] = ['in',$course_res_str];
            }else if(get_current_group_type() == 4){ //班主任
                $grades_res = $this->gradesdetailModel->where(['user_master_id' => get_current_id()])->column('id');
                $grades_res_str = implode(',',$grades_res);
                $where_elimination['class_subject_id'] = ['in',$grades_res_str];
                $where_['class_subject_id'] = ['in',$grades_res_str];
				$where_grades['id'] = ['in',$grades_res_str];

                $course_res = $this->gradesdetailModel->where(['user_master_id' => get_current_id()])->column('course_id');
                $course_res_str = implode(',',$course_res);
                $where_course['id'] = ['in',$course_res_str];
            }else if(get_current_group_type() == 5){ //教务类
                $grades_res = $this->gradesdetailModel->where(['user_edu_id' => get_current_id()])->column('id');
                $grades_res_str = implode(',',$grades_res);
                $where_elimination['class_subject_id'] = ['in',$grades_res_str];
                $where_['class_subject_id'] = ['in',$grades_res_str];
				$where_grades['id'] = ['in',$grades_res_str];

                $course_res = $this->gradesdetailModel->where(['user_edu_id' => get_current_id()])->column('course_id');
                $course_res_str = implode(',',$course_res);
                $where_course['id'] = ['in',$course_res_str];
            }
        }

        foreach ($campus_name as $key => $value){
            $item = [];
            $where_elimination['campus_id'] = $value['id'];
            $campus_elimination_sum = $this->studentsubjectinfosclassModel->where($where_elimination)->sum('eliminated_nums'); //该校区课消总数
            $campus_elimination_amount = $this->studentsubjectinfosclassModel->where($where_elimination)->sum('amount_deducted'); //该校区课消总金额
            $item['elimination_sum'] = $campus_elimination_sum;
            $item['elimination_amount'] = $campus_elimination_amount /100/ 1000;
            $item['campus_name'] = $value['campus_name'];
            $data_campus_elimination[] = $item;
        }


        if(get_current_type() == 4 || get_current_type() == 5){/*校区数据权限*/
            $where_['campus_id'] = get_current_campus_id();
            $where_course['campus_id'] = get_current_campus_id();
			$where_grades['campus_id'] = get_current_campus_id();
        }elseif (get_current_type() == 2 || get_current_type() == 3){/*区域数据权限*/
            $where_['campus_id'] = ['in',get_current_campus_id()];
            $where_course['campus_id'] = ['in',get_current_campus_id()];
			$where_grades['campus_id'] = ['in',get_current_campus_id()];
        }



        $elimination_sum = $this->studentsubjectinfosclassModel->where($where_)->sum('eliminated_nums'); //课消总数
        $elimination_amount = $this->studentsubjectinfosclassModel->where($where_)->sum('amount_deducted') / 100/1000; //课消总金额
        $data_campus_elimination = json_encode($data_campus_elimination);

        //班级
        unset($where_['status']);
        unset($where_['give_status']);
        unset($where_grades['status']);
        unset($where_grades['give_status']);
        $grades_list = $this->gradesdetailModel->where($where_grades)->select();
        //课程
        $course_list = $this->courseModel->where($where_course)->select();

        $this->dictionary();
        return $this->fetch('',['course_list' => $course_list, 'grades_list' => $grades_list, 'elimination_sum' => $elimination_sum, 'elimination_amount' => $elimination_amount,  'data_campus_elimination' => $data_campus_elimination, 'campus_name' => $campus_name]);
    }




    //校区列表
    public function eliminationlists(){
        if ($this->request->isAjax()) {
            $param = $this->request->param();
            $result = $this->campusModel->getEliminationLists($param);
            $this->success('success', '', $result, 0);
        }

    }

    //课程列表
    public function eliminationcourselists(){
        if ($this->request->isAjax()) {
            $param = $this->request->param();
            $result = $this->courseModel->getEliminationLists($param,input('campus_id'));
            $this->success('success', '', $result, 0);
        }
    }


    public function class_report(){

        //课程
        $campus_id = input('id');
        $course_result = $this->courseModel->where(['campus_id' => $campus_id])->select();
        $data_course_elimination = [];

        $where = [];
        $where_ = [];
        $where['status'] = 1;
        $where['give_status'] = 1;    //赠送不算课消
        $where_['status'] = 1;
        $where_['give_status'] = 1;    //赠送不算课消
        $where_['campus_id'] = $campus_id;
        if(get_current_type() == 5){
            if(get_current_group_type() == 2){  //老师类
                $grades_res = $this->gradesdetailModel->where(['user_teacher_id' => get_current_id()])->column('id');
                $grades_res_str = implode(',',$grades_res);
                $where['class_subject_id'] = ['in',$grades_res_str];
                $where_['class_subject_id'] = ['in',$grades_res_str];
            }else if(get_current_group_type() == 3){  //助教类
                $grades_res = $this->gradesdetailModel->where(['user_assistant_id' => get_current_id()])->column('id');
                $grades_res_str = implode(',',$grades_res);
                $where['class_subject_id'] = ['in',$grades_res_str];
                $where_['class_subject_id'] = ['in',$grades_res_str];
            }else if(get_current_group_type() == 4){ //班主任
                $grades_res = $this->gradesdetailModel->where(['user_master_id' => get_current_id()])->column('id');
                $grades_res_str = implode(',',$grades_res);
                $where['class_subject_id'] = ['in',$grades_res_str];
                $where_['class_subject_id'] = ['in',$grades_res_str];
            }else if(get_current_group_type() == 5){ //教务类
                $grades_res = $this->gradesdetailModel->where(['user_edu_id' => get_current_id()])->column('id');
                $grades_res_str = implode(',',$grades_res);
                $where['class_subject_id'] = ['in',$grades_res_str];
                $where_['class_subject_id'] = ['in',$grades_res_str];
            }
        }

        foreach ($course_result as $key => $value){
            $item = [];
            $where['course_id'] = $value['id'];
            $course_elimination_sum = $this->studentsubjectinfosclassModel->where($where)->sum('eliminated_nums'); //该课程课消总数
            $course_elimination_amount = $this->studentsubjectinfosclassModel->where($where)->sum('amount_deducted'); //该课程课消总金额
            $item['elimination_sum'] = $course_elimination_sum;
            $item['elimination_amount'] = $course_elimination_amount /100/ 1000;
            $item['course_name'] = $value['course_name'];
            $data_course_elimination[] = $item;
        }

        $elimination_sum = $this->studentsubjectinfosclassModel->where($where_)->sum('eliminated_nums'); //课消总数
        $elimination_amount = $this->studentsubjectinfosclassModel->where($where_)->sum('amount_deducted') /100/ 1000; //课消总金额
        $data_course_elimination = json_encode($data_course_elimination);

        return $this->fetch('',['campus_id' => $campus_id ,'course' => $course_result, 'elimination_sum' => $elimination_sum, 'elimination_amount' => $elimination_amount,  'data_course_elimination' => $data_course_elimination,]);
    }

    //校区
    public function searcheliminationlists(){
        if ($this->request->isAjax()) {
            $data =  input('post.');
            $where_time = ['status' => 1]; //已上课
            $where = [];
            if (isset($data['campus_name']) && '' !== $data['campus_name']) {  //校区名字
                $where['campus_name'] = ['like', '%'.$data['campus_name'].'%'];
            }

            if(get_current_type() == 4 || get_current_type() == 5){
                $where['id'] = get_current_campus_id();
            }elseif (get_current_type() == 2 || get_current_type() == 3){
                $where['id'] = ['in',get_current_campus_id()];
            }

            //时间段
            if (isset($data['start_date']) && '' !== $data['start_date'] && isset($data['end_date']) && '' !== $data['end_date']) {
                $where_time['class_date'] = array('between', array($data['start_date'],$data['end_date']));
            }
            $campus_result = $this->campusModel->where($where)->select();
            $data_campus_elimination = [];
            $elimination_data_sum = 0;
            $campus_elimination_data_amount = 0;


            if(get_current_type() == 5){
                if(get_current_group_type() == 2){  //老师类
                    $grades_res = $this->gradesdetailModel->where(['user_teacher_id' => get_current_id()])->column('id');
                    $grades_res_str = implode(',',$grades_res);
                    $where_time['class_subject_id'] = ['in',$grades_res_str];
                }else if(get_current_group_type() == 3){  //助教类
                    $grades_res = $this->gradesdetailModel->where(['user_assistant_id' => get_current_id()])->column('id');
                    $grades_res_str = implode(',',$grades_res);
                    $where_time['class_subject_id'] = ['in',$grades_res_str];
                }else if(get_current_group_type() == 4){ //班主任
                    $grades_res = $this->gradesdetailModel->where(['user_master_id' => get_current_id()])->column('id');
                    $grades_res_str = implode(',',$grades_res);
                    $where_time['class_subject_id'] = ['in',$grades_res_str];
                }else if(get_current_group_type() == 5){ //教务类
                    $grades_res = $this->gradesdetailModel->where(['user_edu_id' => get_current_id()])->column('id');
                    $grades_res_str = implode(',',$grades_res);
                    $where_time['class_subject_id'] = ['in',$grades_res_str];
                }
            }


            foreach ($campus_result as $key => $value){
                $where_time['campus_id'] = $value['id'];
                $item = [];
                $campus_elimination_sum = $this->studentsubjectinfosclassModel->where($where_time)->sum('eliminated_nums'); //该校区课消总数
                $campus_elimination_amount = $this->studentsubjectinfosclassModel->where($where_time)->sum('amount_deducted'); //该校区课消总金额
                $item['elimination_sum'] = $campus_elimination_sum;
                $elimination_data_sum = $elimination_data_sum + $item['elimination_sum'];
                $item['elimination_amount'] = $campus_elimination_amount / 1000;
                $campus_elimination_data_amount = $campus_elimination_data_amount +  $item['elimination_amount'];
                $item['campus_name'] = $value['campus_name'];
                $data_campus_elimination[] = $item;
            }
            $return_data = [];
            $return_data['campus_elimination_sum'] = $elimination_data_sum;//课消总数
            $return_data['campus_elimination_amount'] = $campus_elimination_data_amount;//课消总金额
            $return_data['campus_elimination_json'] = $data_campus_elimination;
            $return_data = json_encode($return_data);


            $this->success('success', '', $return_data, 0);
        }

    }

    //课程
    public function searcheliminationcourselists(){
        if ($this->request->isAjax()) {
            $data =  input('post.');
            $campus_id = input('campus_id');
            $where = ['campus_id' => $campus_id];
            $where_time = ['status' => 1,'campus_id' => $campus_id]; //已上课


            if (isset($data['course_name']) && '' !== $data['course_name']) {  //课程名字
                $where['course_name'] = ['like', '%'.$data['course_name'].'%'];
            }

            //时间段
            if (isset($data['start_date']) && '' !== $data['start_date'] && isset($data['end_date']) && '' !== $data['end_date']) {
                $where_time['class_date'] = array('between', array($data['start_date'],$data['end_date']));
            }
            $course_result = $this->courseModel->where($where)->select();
            $data_course_elimination = [];
            $elimination_data_sum = 0;
            $course_elimination_data_amount = 0;

            if(get_current_type() == 5){
                if(get_current_group_type() == 2){  //老师类
                    $grades_res = $this->gradesdetailModel->where(['user_teacher_id' => get_current_id()])->column('id');
                    $grades_res_str = implode(',',$grades_res);
                    $where_time['class_subject_id'] = ['in',$grades_res_str];
                }else if(get_current_group_type() == 3){  //助教类
                    $grades_res = $this->gradesdetailModel->where(['user_assistant_id' => get_current_id()])->column('id');
                    $grades_res_str = implode(',',$grades_res);
                    $where_time['class_subject_id'] = ['in',$grades_res_str];
                }else if(get_current_group_type() == 4){ //班主任
                    $grades_res = $this->gradesdetailModel->where(['user_master_id' => get_current_id()])->column('id');
                    $grades_res_str = implode(',',$grades_res);
                    $where_time['class_subject_id'] = ['in',$grades_res_str];
                }else if(get_current_group_type() == 5){ //教务类
                    $grades_res = $this->gradesdetailModel->where(['user_edu_id' => get_current_id()])->column('id');
                    $grades_res_str = implode(',',$grades_res);
                    $where_time['class_subject_id'] = ['in',$grades_res_str];
                }
            }


            foreach ($course_result as $key => $value){
                $where_time['course_id'] = $value['id'];
                $item = [];
                $course_elimination_sum = $this->studentsubjectinfosclassModel->where($where_time)->sum('eliminated_nums'); //该校区课消总数
                $course_elimination_amount = $this->studentsubjectinfosclassModel->where($where_time)->sum('amount_deducted'); //该校区课消总金额
                $item['elimination_sum'] = $course_elimination_sum;
                $elimination_data_sum = $elimination_data_sum + $item['elimination_sum'];
                $item['elimination_amount'] = $course_elimination_amount / 1000;
                $course_elimination_data_amount = $course_elimination_data_amount +  $item['elimination_amount'];
                $item['course_name'] = $value['course_name'];
                $data_course_elimination[] = $item;
            }
            $return_data = [];
            $return_data['course_elimination_sum'] = $elimination_data_sum;//课消总数
            $return_data['course_elimination_amount'] = $course_elimination_data_amount;//课消总金额
            $return_data['course_elimination_json'] = $data_course_elimination;
            $return_data = json_encode($return_data);


            $this->success('success', '', $return_data, 0);
        }

    }


    public function elimination_report(){
        $course_id = input('course_id');
        $course_name = input('course_name');
        $campus_id = input('campus_id');
        $start_date = input('start_date');
        $end_date = input('end_date');
        return $this->fetch('',['campus_id' => $campus_id ,'course_id' => $course_id, 'course_name' => $course_name, 'start_date' => $start_date, 'end_date' => $end_date]);
    }


    public function geteliminationlists(){
        if ($this->request->isAjax()) {
            $course_id = input('course_id');
            $start_date = input('start_date');
            $end_date = input('end_date');
            $campus_id = input('campus_id');
            $param['campus_id']  = $campus_id;
            $param['start_date'] = $start_date;
            $param['end_date']  = $end_date;
            $param = $this->request->param();
            $result = $this->studentsubjectinfosclassModel->getCourseEliminationLists($param,$course_id);
            $this->success('success', '', $result, 0);
        }
    }

    /**
     *  实例化.
     */
    protected function _initialize()
    {
        parent::_initialize();
        $this->studentsubjectinfosclassModel = new StudentSubjectInfosClassModel();
        $this->campusModel = new CampusModel();
        $this->courseModel = new CourseModel();
        $this->gradesdetailModel = new GradesDetailsModel();
        $this->studentModel = new StudentModel();
        $this->teacherModel = new TeacherModel();
        $this->userModel = new UserModel();
    }
}
